<?php

namespace App\Http\Controllers;

use App\Models\Department;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */


    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::getAllInfo();
        return view('akl.admin.admin_index')->with('users',$users);
    }

    public function config(){
//        dd( explode('/',"http://77fmtb.com1.z0.glb.clouddn.com/gogopher.jpg?watermark/2/text/5LiD54mb5LqR5a2Y5YKo/font/5a6L5L2T/fontsize/1000/fill/#000000=/dissolve/85/gravity/SouthEast/dx/20/dy/20)" ));
//        dd(base64_encode('混蛋'));
        return view('akl.admin.config_index');
    }

    public function store(Request $request){
        $inp = array(
            "name" => $request->all()['name'],
            "introduce" => $request->all()['introduce'],
            "botton" => $request->all()['botton'],
            "water" => $request->all()['water'],
        );
        if ($request->get('imgs')[0]) $inp['logo'] = $request->get('imgs')[0];
        $rel = Department::where('id',Auth::user()->partid)->update($inp);
        if (!$rel) return back()->with('errors','更新失败');
        $this->resetsession();
        return back()->with('errors','更新成功');
    }

    public function resetsession(){
        Session::forget('domain');
        session()->forget('domain');
        $url = preg_replace("/(lara4.com).+/", "lara4.com", url()->current(),1);
        $url = preg_replace("/http:\/\//", "", $url,1);
        $depart = Department::where(['domain'=>$url,'active'=>'0'])->first();
        if(!$depart) return back()->with('errors','此域名不存在');
        Session::put('doain', $depart);
        session()->put('domain', $depart);
    }


    public function showLogin(){
        return view('akl.login');
    }

    public function postLogin(){
        return redirect('index');
    }

    public function logout(){
        Auth::logout();
        return redirect('login');
    }


}
